/*
 * SwingMetaObject.cpp  0.4.0 / Nov 17, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "SwingMetaObject.h"

#include "ColorChooserDelegate.h"
#include "DesktopDelegate.h"
#include "FileChooserDelegate.h"
#include "FontChooserDelegate.h"
#include "OptionPaneDelegate.h"
#include "ToolTipDelegate.h"
#include "WindowDelegate.h"
#include "SwingImageDelegate.h"
#include "WhatsThisManagerDelegate.h"

// SwingMetaObject :: Helper Macros. -------------------------------------------

#define FRAME_RET(type) (*reinterpret_cast<type(*)>(frame->ret->data()))
#define FRAME_ARG(type, num) \
    (*reinterpret_cast<type(*)>(frame->val##num->data()))

// SwingMetaObject :: Private Constructors. ------------------------------------

SwingMetaObject::SwingMetaObject()
  : QObject(0)
{
  connect(this, SIGNAL(invokeMemberSignal(InvokeMemberFrame *)), this,
          SLOT(invokeMemberSlot(InvokeMemberFrame *)),
          Qt::BlockingQueuedConnection);
  connect(this, SIGNAL(invokeStaticMemberSignal(InvokeStaticMemberFrame *)),
          this, SLOT(invokeStaticMemberSlot(InvokeStaticMemberFrame *)),
          Qt::BlockingQueuedConnection);
}

SwingMetaObject::SwingMetaObject(const SwingMetaObject &)
  : QObject(0)
{
  // disabled
}

// SwingMetaObject :: Private Destructor. --------------------------------------

SwingMetaObject::~SwingMetaObject()
{
  // disabled
}

// SwingMetaObject :: Public Static Member Function. ---------------------------

/*!
 * \brief Initializes SwingMetaObject class.
 */
void SwingMetaObject::initialize()
{
  Q_ASSERT(!sm_instance);
  sm_instance = new SwingMetaObject();
}

/*!
 * \brief Invokes member function of the specified objet.
 *
 * \param obj the QObject
 * \param member the member name
 * \param type the return type
 * \param ret the return value
 * \param val0 the argument
 * \param val1 the argument
 * \param val2 the argument
 * \param val3 the argument
 * \param val4 the argument
 * \param val5 the argument
 * \param val6 the argument
 * \param val7 the argument
 * \param val8 the argument
 * \param val9 the argument
 *
 * \return \c true if succeed result
 */
bool SwingMetaObject::invokeMember(QObject *obj, const char *member,
                                   const char *type, void *ret,
                                   QGenericArgument *val0,
                                   QGenericArgument *val1,
                                   QGenericArgument *val2,
                                   QGenericArgument *val3,
                                   QGenericArgument *val4,
                                   QGenericArgument *val5,
                                   QGenericArgument *val6,
                                   QGenericArgument *val7,
                                   QGenericArgument *val8,
                                   QGenericArgument *val9)
{
  Q_ASSERT(obj);
  Q_ASSERT(member);
  QGenericReturnArgument gra(type, ret);
  InvokeMemberFrame frame;
  frame.obj = obj;
  frame.member = member;
  frame.ret = &gra;
  frame.val0 = val0;
  frame.val1 = val1;
  frame.val2 = val2;
  frame.val3 = val3;
  frame.val4 = val4;
  frame.val5 = val5;
  frame.val6 = val6;
  frame.val7 = val7;
  frame.val8 = val8;
  frame.val9 = val9;
  sm_instance->invokeMember(&frame);
  return frame.succeed;
}

/*!
 * \brief Invokes static member function of the specified class.
 *
 * \param classID the class ID
 * \param methodID the method ID
 * \param type the return type
 * \param ret the return value
 * \param val0 the argument
 * \param val1 the argument
 * \param val2 the argument
 * \param val3 the argument
 * \param val4 the argument
 * \param val5 the argument
 * \param val6 the argument
 * \param val7 the argument
 * \param val8 the argument
 * \param val9 the argument
 *
 * \return \c true if succeed result
 */
bool SwingMetaObject::invokeStaticMember(int classID, int methodID,
                                         const char *type, void *ret,
                                         QGenericArgument *val0,
                                         QGenericArgument *val1,
                                         QGenericArgument *val2,
                                         QGenericArgument *val3,
                                         QGenericArgument *val4,
                                         QGenericArgument *val5,
                                         QGenericArgument *val6,
                                         QGenericArgument *val7,
                                         QGenericArgument *val8,
                                         QGenericArgument *val9)
{
  QGenericReturnArgument gra(type, ret);
  InvokeStaticMemberFrame frame;
  frame.classID = classID;
  frame.methodID = methodID;
  frame.ret = &gra;
  frame.val0 = val0;
  frame.val1 = val1;
  frame.val2 = val2;
  frame.val3 = val3;
  frame.val4 = val4;
  frame.val5 = val5;
  frame.val6 = val6;
  frame.val7 = val7;
  frame.val8 = val8;
  frame.val9 = val9;
  sm_instance->invokeStaticMember(&frame);
  return frame.succeed;
}

/*!
 * \brief Invokes static member function of the specified class.
 *
 * \param classID the class ID
 * \param methodID the method ID
 * \param val0 the argument
 * \param val1 the argument
 * \param val2 the argument
 * \param val3 the argument
 * \param val4 the argument
 * \param val5 the argument
 * \param val6 the argument
 * \param val7 the argument
 * \param val8 the argument
 * \param val9 the argument
 *
 * \return \c true if succeed result
 */
bool SwingMetaObject::invokeStaticMember(int classID, int methodID,
                                         QGenericArgument val0,
                                         QGenericArgument val1,
                                         QGenericArgument val2,
                                         QGenericArgument val3,
                                         QGenericArgument val4,
                                         QGenericArgument val5,
                                         QGenericArgument val6,
                                         QGenericArgument val7,
                                         QGenericArgument val8,
                                         QGenericArgument val9)
{
  InvokeStaticMemberFrame frame;
  frame.classID = classID;
  frame.methodID = methodID;
  frame.val0 = &val0;
  frame.val1 = &val1;
  frame.val2 = &val2;
  frame.val3 = &val3;
  frame.val4 = &val4;
  frame.val5 = &val5;
  frame.val6 = &val6;
  frame.val7 = &val7;
  frame.val8 = &val8;
  frame.val9 = &val9;
  sm_instance->invokeStaticMember(&frame);
  return frame.succeed;
}

// SwingMetaObject :: Private Member Functions. --------------------------------

void SwingMetaObject::invokeMember(InvokeMemberFrame *frame)
{
  emit invokeMemberSignal(frame);
}

void SwingMetaObject::invokeStaticMember(InvokeStaticMemberFrame *frame)
{
  emit invokeStaticMemberSignal(frame);
}

void SwingMetaObject::invokeMemberSlot(InvokeMemberFrame *frame)
{
  frame->succeed = QMetaObject::invokeMethod(frame->obj, frame->member,
                                             Qt::DirectConnection, *frame->ret,
                                             *frame->val0, *frame->val1,
                                             *frame->val2, *frame->val3,
                                             *frame->val4, *frame->val5,
                                             *frame->val6, *frame->val7,
                                             *frame->val8, *frame->val9); 
}

void SwingMetaObject::invokeStaticMemberSlot(InvokeStaticMemberFrame *frame)
{
  switch(frame->classID) {
// OptionPaneDelegate Static Member Functions
    case OptionPaneDelegate_classID:
      switch(frame->methodID) {
        case OptionPaneDelegate_showMessage_methodID:
          FRAME_RET(jint) =
                        OptionPaneDelegate::showMessage(FRAME_ARG(JNIEnv *, 0),
                                                        FRAME_ARG(jclass, 1),
                                                        FRAME_ARG(jint, 2),
                                                        FRAME_ARG(jlong, 3),
                                                        FRAME_ARG(jstring, 4),
                                                        FRAME_ARG(jstring, 5),
                                                        FRAME_ARG(jint, 6),
                                                        FRAME_ARG(jint, 7));
          frame->succeed = true;
          break;
        default:
          frame->succeed = false;
      }
      break;
// WindowDelegate Static Member Functions
    case WindowDelegate_classID:
      switch(frame->methodID) {
        case WindowDelegate_getWindows_methodID:
          FRAME_RET(jobjectArray) =
                             WindowDelegate::getWindows(FRAME_ARG(JNIEnv *, 0),
                                                        FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        case WindowDelegate_getActiveWindow_methodID:
          FRAME_RET(jobject) =
                        WindowDelegate::getActiveWindow(FRAME_ARG(JNIEnv *, 0),
                                                        FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        case WindowDelegate_setWindowOwner_methodID:
          WindowDelegate::setWindowOwner(FRAME_ARG(JNIEnv *, 0),
                                         FRAME_ARG(jclass, 1),
                                         FRAME_ARG(jlong, 2),
                                         FRAME_ARG(jlong, 3));
          frame->succeed = true;
          break;
        default:
          frame->succeed = false;
      }
      break;
// ColorChooserDelegate Static Member Functions
    case ColorChooserDelegate_classID:
      switch(frame->methodID) {
        case ColorChooserDelegate_getColor_methodID:
          FRAME_RET(jobject) =
                         ColorChooserDelegate::getColor(FRAME_ARG(JNIEnv *, 0),
                                                        FRAME_ARG(jclass, 1),
                                                        FRAME_ARG(jlong, 2),
                                                        FRAME_ARG(jint, 3));
          frame->succeed = true;
          break;
        default:
          frame->succeed = false;
      }
      break;
// FontChooserDelegate Static Member Functions
    case FontChooserDelegate_classID:
      switch(frame->methodID) {
        case FontChooserDelegate_getFont_methodID:
          FRAME_RET(jobject) =
                           FontChooserDelegate::getFont(FRAME_ARG(JNIEnv *, 0),
                                                        FRAME_ARG(jclass, 1),
                                                        FRAME_ARG(jlong, 2),
                                                        FRAME_ARG(jobject, 3),
                                                        FRAME_ARG(jstring, 4));
          frame->succeed = true;
          break;
        default:
          frame->succeed = false;
      }
      break;
// FileChooserDelegate Static Member Functions
    case FileChooserDelegate_classID:
      switch(frame->methodID) {
        case FileChooserDelegate_getExistingDirectoryName_methodID:
          FRAME_RET(jstring) =
                          FileChooserDelegate::getExistingDirectoryName(
                                                  FRAME_ARG(JNIEnv *, 0),
                                                  FRAME_ARG(jclass, 1),
                                                  FRAME_ARG(jlong, 2),
                                                  FRAME_ARG(jstring, 3),
                                                  FRAME_ARG(jstring, 4));
          frame->succeed = true;
          break;
        case FileChooserDelegate_getOpenFileName_methodID:
          FRAME_RET(jstring) =
                   FileChooserDelegate::getOpenFileName(FRAME_ARG(JNIEnv *, 0),
                                                        FRAME_ARG(jclass, 1),
                                                        FRAME_ARG(jlong, 2),
                                                        FRAME_ARG(jstring, 3),
                                                        FRAME_ARG(jstring, 4),
                                                        FRAME_ARG(jstring, 5),
                                                        FRAME_ARG(jstring, 6));
          frame->succeed = true;
          break;
        case FileChooserDelegate_getSaveFileName_methodID:
          FRAME_RET(jstring) =
                   FileChooserDelegate::getSaveFileName(FRAME_ARG(JNIEnv *, 0),
                                                        FRAME_ARG(jclass, 1),
                                                        FRAME_ARG(jlong, 2),
                                                        FRAME_ARG(jstring, 3),
                                                        FRAME_ARG(jstring, 4),
                                                        FRAME_ARG(jstring, 5),
                                                        FRAME_ARG(jstring, 6));
          frame->succeed = true;
          break;
        default:
          frame->succeed = false;
      }
      break;
// ToolTipDelegate Static Member Functions
    case ToolTipDelegate_classID:
      switch(frame->methodID) {
        case ToolTipDelegate_setFont_methodID:
          ToolTipDelegate::setFont(FRAME_ARG(JNIEnv *, 0), FRAME_ARG(jclass, 1),
                                   FRAME_ARG(jobject, 2));
          frame->succeed = true;
          break;
        case ToolTipDelegate_setBackground_methodID:
          ToolTipDelegate::setBackground(FRAME_ARG(JNIEnv *, 0),
                                         FRAME_ARG(jclass, 1),
                                         FRAME_ARG(jint, 2));
          frame->succeed = true;
          break;
        case ToolTipDelegate_setNullBackground_methodID:
          ToolTipDelegate::setNullBackground(FRAME_ARG(JNIEnv *, 0),
                                             FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        case ToolTipDelegate_setForeground_methodID:
          ToolTipDelegate::setForeground(FRAME_ARG(JNIEnv *, 0),
                                         FRAME_ARG(jclass, 1),
                                         FRAME_ARG(jint, 2));
          frame->succeed = true;
          break;
        case ToolTipDelegate_setNullForeground_methodID:
          ToolTipDelegate::setNullForeground(FRAME_ARG(JNIEnv *, 0),
                                             FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        case ToolTipDelegate_showText_methodID:
          ToolTipDelegate::showText(FRAME_ARG(JNIEnv *, 0),
                                    FRAME_ARG(jclass, 1), FRAME_ARG(jstring, 2),
                                    FRAME_ARG(jint, 3), FRAME_ARG(jint, 4),
                                    FRAME_ARG(jlong, 5));
          frame->succeed = true;
          break;
        case ToolTipDelegate_hideText_methodID:
          ToolTipDelegate::hideText(FRAME_ARG(JNIEnv *, 0),
                                    FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        default:
          frame->succeed = false;
      }
      break;
// WhatsThisManagerDelegate Static Member Functions
    case WhatsThisManagerDelegate_classID:
      switch(frame->methodID) {
        case WhatsThisManagerDelegate_showText_methodID:
          WhatsThisManagerDelegate::showText(FRAME_ARG(JNIEnv *, 0),
                                             FRAME_ARG(jclass, 1),
                                             FRAME_ARG(jstring, 2),
                                             FRAME_ARG(jint, 3),
                                             FRAME_ARG(jint, 4),
                                             FRAME_ARG(jlong, 5));
          frame->succeed = true;
          break;
        case WhatsThisManagerDelegate_hideText_methodID:
          WhatsThisManagerDelegate::hideText(FRAME_ARG(JNIEnv *, 0),
                                             FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        case WhatsThisManagerDelegate_isInWhatsThisMode_methodID:
          FRAME_RET(jboolean) =
            WhatsThisManagerDelegate::isInWhatsThisMode(FRAME_ARG(JNIEnv *, 0),
                                                        FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        case WhatsThisManagerDelegate_enterWhatsThisMode_methodID:
          WhatsThisManagerDelegate::enterWhatsThisMode(FRAME_ARG(JNIEnv *, 0),
                                                       FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        case WhatsThisManagerDelegate_exitWhatsThisMode_methodID:
          WhatsThisManagerDelegate::exitWhatsThisMode(FRAME_ARG(JNIEnv *, 0),
                                                      FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        default:
          frame->succeed = false;
      }
      break;
// SwingImageDelegate Static Member Functions
    case SwingImageDelegate_classID:
      switch(frame->methodID) {
        case SwingImageDelegate_createImage_methodID:
          FRAME_RET(jlong) =
                      SwingImageDelegate::createImage(FRAME_ARG(JNIEnv *, 0));
          frame->succeed = true;
          break;
        case SwingImageDelegate_createImageOBAI_methodID:
          FRAME_RET(jlong) =
                      SwingImageDelegate::createImage(FRAME_ARG(JNIEnv *, 0),
                                                      FRAME_ARG(jobject, 1),
                                                      FRAME_ARG(jbyteArray, 2),
                                                      FRAME_ARG(jint, 3));
          frame->succeed = true;
          break;
        case SwingImageDelegate_createScaled_methodID:
          FRAME_RET(jobject) =
                     SwingImageDelegate::createScaled(FRAME_ARG(JNIEnv *, 0),
                                                      FRAME_ARG(jlong, 1),
                                                      FRAME_ARG(jint, 2),
                                                      FRAME_ARG(jint, 3),
                                                      FRAME_ARG(jint, 4),
                                                      FRAME_ARG(jstring, 5));
          frame->succeed = true;
          break;
        default:
          frame->succeed = false;
      }
      break;
// DesktopDelegate Static Member Functions
    case DesktopDelegate_classID:
      switch(frame->methodID) {
        case DesktopDelegate_getScreenSize_methodID:
          FRAME_RET(jobject) =
                          DesktopDelegate::getScreenSize(FRAME_ARG(JNIEnv *, 0),
                                                         FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        case DesktopDelegate_getScreenResolution_methodID:
          FRAME_RET(int) =
                    DesktopDelegate::getScreenResolution(FRAME_ARG(JNIEnv *, 0),
                                                         FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        case DesktopDelegate_getDesktopBounds_methodID:
          FRAME_RET(jobject) =
                       DesktopDelegate::getDesktopBounds(FRAME_ARG(JNIEnv *, 0),
                                                         FRAME_ARG(jclass, 1));
          frame->succeed = true;
          break;
        case DesktopDelegate_takeScreenshot_methodID:
          FRAME_RET(jobject) =
                         DesktopDelegate::takeScreenshot(FRAME_ARG(JNIEnv *, 0),
                                                         FRAME_ARG(jclass, 1),
                                                         FRAME_ARG(jlong, 2),
                                                         FRAME_ARG(jstring, 3));
          frame->succeed = true;
          break;
        default:
          frame->succeed = false;
      }
      break;
    default:
      frame->succeed = false;
  }
}

// SwingMetaObject :: Private Static Data Member. ------------------------------

SwingMetaObject *SwingMetaObject::sm_instance = 0;
